import { VxeGlobalConfig, VxeGlobalIcon } from 'vxe-table';
import type { VxeComponentSizeType } from 'vxe-table';
// VXE-UI 全局配置统一管理
// 重置图标
export const vxeUIGlobICON: VxeGlobalIcon = {
  // table 对应 Element Plus 图标
  TABLE_SORT_ASC: 'vxe-icon-caret-top',
  TABLE_SORT_DESC: 'vxe-icon-caret-bottom',
  TABLE_FILTER_NONE: 'vxe-icon-funnel',
  TABLE_FILTER_MATCH: 'vxe-icon-funnel',
  TABLE_EDIT: 'vxe-icon-edit',
  TABLE_TITLE_PREFIX: 'vxe-icon-question-circle-fill',
  TABLE_TITLE_SUFFIX: 'vxe-icon-question-circle-fill',
  TABLE_TREE_LOADED: 'vxe-icon-spinner roll',
  TABLE_TREE_OPEN: 'vxe-icon-caret-right rotate90',
  TABLE_TREE_CLOSE: 'vxe-icon-caret-right',
  TABLE_EXPAND_LOADED: 'vxe-icon-spinner roll',
  TABLE_EXPAND_OPEN: 'vxe-icon-arrow-right rotate90',
  TABLE_EXPAND_CLOSE: 'vxe-icon-arrow-right',
  TABLE_CHECKBOX_CHECKED: 'vxe-icon-checkbox-checked-fill',
  TABLE_CHECKBOX_UNCHECKED: 'vxe-icon-checkbox-unchecked',
  TABLE_CHECKBOX_INDETERMINATE: 'vxe-icon-checkbox-indeterminate-fill',
  TABLE_RADIO_CHECKED: 'vxe-icon-radio-checked-fill',
  TABLE_RADIO_UNCHECKED: 'vxe-icon-radio-unchecked',
  TABLE_CUSTOM_SORT: 'vxe-icon-drag-handle',
  TABLE_MENU_OPTIONS: 'vxe-icon-arrow-right',
  TABLE_DRAG_ROW: 'vxe-icon-drag-handle',
  TABLE_DRAG_COLUMN: 'vxe-icon-drag-handle',

  // toolbar
  TOOLBAR_TOOLS_REFRESH: 'vxe-icon-repeat',
  TOOLBAR_TOOLS_REFRESH_LOADING: 'vxe-icon-repeat roll',
  TOOLBAR_TOOLS_IMPORT: 'vxe-icon-upload',
  TOOLBAR_TOOLS_EXPORT: 'vxe-icon-download',
  TOOLBAR_TOOLS_PRINT: 'vxe-icon-print',
  TOOLBAR_TOOLS_FULLSCREEN: 'vxe-icon-fullscreen',
  TOOLBAR_TOOLS_MINIMIZE: 'vxe-icon-minimize',
  TOOLBAR_TOOLS_CUSTOM: 'vxe-icon-custom-column',
  TOOLBAR_TOOLS_FIXED_LEFT: 'vxe-icon-fixed-left',
  TOOLBAR_TOOLS_FIXED_LEFT_ACTIVE: 'vxe-icon-fixed-left-fill',
  TOOLBAR_TOOLS_FIXED_RIGHT: 'vxe-icon-fixed-right',
  TOOLBAR_TOOLS_FIXED_RIGHT_ACTIVE: 'vxe-icon-fixed-right-fill',
};
const sizeEnum: Record<string, VxeComponentSizeType> = {
  default: 'small',
  medium: 'medium',
  small: 'small',
  mini: 'mini',
};
// 全局vxe 配置
export const vxeTableGlobConfig: VxeGlobalConfig = {
  modal: {
    width: 800,
    // 是否拖动
    draggable: false,
  },
  size: sizeEnum['small'],
  table: {
    height: 'auto',
    emptyRender: {
      name: 'RenderEmpty',
      props: {
        description: '暂无数据~',
      },
    },
    // emptyText: '暂无数据~',
    align: 'center',
    showOverflow: 'tooltip',
    headerAlign: 'center',
    border: true,
    padding: true,
    stripe: false,
    columnConfig: {
      // isCurrent: true,
      // isHover: true,
      resizable: true,
      // drag: true,
    },

    rowConfig: {
      height: 48,
      resizable: true,
      // isCurrent: true,
      isHover: true,
    },
    // 虚拟滚动
    virtualYConfig: { enabled: true, gt: 0 },
    virtualXConfig: { enabled: true, gt: 0 },
    exportConfig: {
      includeFields: [],
      // 过滤操作列表
      columnFilterMethod({ column }) {
        return (
          column.field !== 'action' &&
          !['操作'].includes(column.title) &&
          !['seq', 'radio', 'checkbox', 'expand'].includes(column.type as string)
        );
      },
      columns: [],
      types: ['csv', 'html', 'xml', 'txt'],
    },
    printConfig: {},
  },
  grid: {
    customConfig: {
      // 保存本地
      storage: true,
      // mode: "popup",
    },

    formConfig: {
      // className: 'p-b-10px',
    },
    toolbarConfig: {
      className: 'form-p-b-10px',
      refresh: true, // 显示刷新按钮
      enabled: true,
      zoom: true, // 显示全屏按钮
      custom: true, // 显示自定义列按钮
      // import: true, // 显示导入按钮
      export: true, // 显示导出按钮
      // print: true, // 显示打印按钮
    },

    zoomConfig: {
      escRestore: true,
    },
    pagerConfig: {
      enabled: true,
      background: true,
      pageSizes: [10, 20, 30, 50, 100],
      pageSize: 10,
      align: 'right',
      border: true,
      layouts: ['Total', 'Sizes', 'Home', 'PrevPage', 'JumpNumber', 'NextPage', 'End', 'FullJump'],
    },
    proxyConfig: {
      seq: true, // 启用动态序号代理，每一页的序号会根据当前页数变化
      // sort: true, // 启用排序代理，当点击排序时会自动触发 query 行为
      filter: true, // 启用筛选代理，当点击筛选时会自动触发 query 行为
      form: true, // 启用表单代理，当点击表单提交按钮时会自动触发 reload 行为
      showResponseMsg: true, // 是否显示服务端错误信息
      props: {
        // 旧版本配置已经废弃了
        // result: "rows", // 配置响应结果列表字段
        // total: "total", // 配置响应结果总页数字段
      },
      response: {
        // 配置响应结果列表字段
        list: ({ data }) => {
          // 支持所有不同的响应结构
          let result = [];

          try {
            if (data && typeof data === 'object' && !Array.isArray(data)) {
              if (data.data && typeof data.data === 'object') {
                if (data.data.records && Array.isArray(data.data.records)) {
                  result = data.data.records;
                } else if (data.data.list && Array.isArray(data.data.list)) {
                  result = data.data.list;
                } else if (Array.isArray(data.data)) {
                  result = data.data;
                }
              } else if (data.rows && Array.isArray(data.rows)) {
                result = data.rows;
              } else if (data.list && Array.isArray(data.list)) {
                result = data.list;
              } else if (data.records && Array.isArray(data.records)) {
                result = data.records;
              } else if (data.result && Array.isArray(data.result)) {
                result = data.result;
              }
            } else if (Array.isArray(data)) {
              result = data;
            }
          } catch (e) {
            console.error('VXE Table data parsing error:', e);
            result = [];
          }

          return result;
        },
        result: ({ data }) => {
          // 支持所有不同的响应结构
          let result = [];

          try {
            if (data && typeof data === 'object' && !Array.isArray(data)) {
              if (data.data && typeof data.data === 'object') {
                if (data.data.records && Array.isArray(data.data.records)) {
                  result = data.data.records;
                } else if (data.data.list && Array.isArray(data.data.list)) {
                  result = data.data.list;
                } else if (Array.isArray(data.data)) {
                  result = data.data;
                }
              } else if (data.rows && Array.isArray(data.rows)) {
                result = data.rows;
              } else if (data.list && Array.isArray(data.list)) {
                result = data.list;
              } else if (data.records && Array.isArray(data.records)) {
                result = data.records;
              } else if (data.result && Array.isArray(data.result)) {
                result = data.result;
              }
            } else if (Array.isArray(data)) {
              result = data;
            }
          } catch (e) {
            console.error('VXE Table data parsing error:', e);
            result = [];
          }

          return result;
        },

        // 配置响应结果总页数字段
        total: ({ data }) => {
          let total = 0;

          try {
            if (data && typeof data === 'object') {
              if (data.data && typeof data.data === 'object') {
                if (data.data.total !== undefined && !isNaN(Number(data.data.total))) {
                  total = Number(data.data.total);
                } else if (
                  data.data.page &&
                  typeof data.data.page === 'object' &&
                  data.data.page.total !== undefined &&
                  !isNaN(Number(data.data.page.total))
                ) {
                  total = Number(data.data.page.total);
                }
              } else if (
                data.page &&
                typeof data.page === 'object' &&
                data.page.total !== undefined &&
                !isNaN(Number(data.page.total))
              ) {
                total = Number(data.page.total);
              } else if (data.total !== undefined && !isNaN(Number(data.total))) {
                total = Number(data.total);
              }
            }
          } catch (e) {
            console.error('VXE Table total parsing error:', e);
            total = 0;
          }

          return total;
        },
      },
    },
  },
};
